home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr54 / bison118.zip / READER.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  40KB  |  1,839 lines

  1. /* Input parser for bison
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* read in the grammar specification and record it in the format described in gram.h.
  22.   All guards are copied into the fguard file and all actions into faction,
  23.   in each case forming the body of a C function (yyguard or yyaction)
  24.   which contains a switch statement to decide which guard or action to execute.
  25.  
  26. The entry point is reader().  */
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include "system.h"
  31. #include "files.h"
  32. #include "new.h"
  33. #include "symtab.h"
  34. #include "lex.h"
  35. #include "gram.h"
  36. #include "machine.h"
  37.  
  38. #define    LTYPESTR    "\n#ifndef YYLTYPE\ntypedef\n  struct yyltype\n\
  39.     {\n      int timestamp;\n      int first_line;\n      int first_column;\
  40. \n      int last_line;\n      int last_column;\n      char *text;\n   }\n\
  41.   yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
  42.  
  43. /* Number of slots allocated (but not necessarily used yet) in `rline'  */
  44. int rline_allocated;
  45.  
  46. extern char *program_name;
  47. extern int definesflag;
  48. extern int nolinesflag;
  49. extern bucket *symval;
  50. extern int numval;
  51. extern int failure;
  52. extern int expected_conflicts;
  53. extern char *token_buffer;
  54.  
  55. #ifndef MSDOS
  56. extern char *realloc ();
  57. #endif
  58.  
  59. extern int atoi ();
  60.  
  61. extern void init_lex();
  62. extern void tabinit();
  63. extern void output_headers();
  64. extern void output_trailers();
  65. extern void free_symtab();
  66. extern void open_extra_files();
  67. extern void fatal();
  68. extern void fatals();
  69. extern void unlex();
  70. extern void done();
  71.  
  72. extern int skip_white_space();
  73. extern int parse_percent_token();
  74. extern int lex();
  75.  
  76. void read_declarations();
  77. void copy_definition();
  78. void parse_token_decl();
  79. void parse_start_decl();
  80. void parse_type_decl();
  81. void parse_assoc_decl();
  82. void parse_union_decl();
  83. void parse_expect_decl();
  84. void copy_action();
  85. void readgram();
  86. void record_rule_line();
  87. void packsymbols();
  88. void output_token_defines();
  89. void packgram();
  90. int read_signed_integer();
  91. int get_type();
  92.  
  93. typedef
  94.   struct symbol_list
  95.     {
  96.       struct symbol_list *next;
  97.       bucket *sym;
  98.       bucket *ruleprec;
  99.     }
  100.   symbol_list;
  101.  
  102.  
  103.  
  104. int lineno;
  105. symbol_list *grammar;
  106. int start_flag;
  107. bucket *startval;
  108. char **tags;
  109.  
  110. /* Nonzero if components of semantic values are used, implying
  111.    they must be unions.  */
  112. static int value_components_used;
  113.  
  114. static int typed;  /* nonzero if %union has been seen.  */
  115.  
  116. static int lastprec;  /* incremented for each %left, %right or %nonassoc seen */
  117.  
  118. static int gensym_count;  /* incremented for each generated symbol */
  119.  
  120. static bucket *errtoken;
  121.  
  122. /* Nonzero if any action or guard uses the @n construct.  */
  123. static int yylsp_needed;
  124.  
  125. void
  126. reader()
  127. {
  128.   start_flag = 0;
  129.   startval = NULL;  /* start symbol not specified yet. */
  130.  
  131. #if 0
  132.   translations = 0;  /* initially assume token number translation not needed.  */
  133. #endif
  134.   /* Nowadays translations is always set to 1,
  135.      since we give `error' a user-token-number
  136.      to satisfy the Posix demand for YYERRCODE==256.  */
  137.   translations = 1;
  138.  
  139.   nsyms = 1;
  140.   nvars = 0;
  141.   nrules = 0;
  142.   nitems = 0;
  143.   rline_allocated = 10;
  144.   rline = NEW2(rline_allocated, short);
  145.  
  146.   typed = 0;
  147.   lastprec = 0;
  148.  
  149.   gensym_count = 0;
  150.  
  151.   semantic_parser = 0;
  152.   pure_parser = 0;
  153.   yylsp_needed = 0;
  154.  
  155.   grammar = NULL;
  156.  
  157.   init_lex();
  158.   lineno = 1;
  159.  
  160.   /* initialize the symbol table.  */
  161.   tabinit();
  162.   /* construct the error token */
  163.   errtoken = getsym("error");
  164.   errtoken->class = STOKEN;
  165.   errtoken->user_token_number = 256; /* Value specified by posix.  */
  166.   /* construct a token that represents all undefined literal tokens. */
  167.   /* it is always token number 2.  */
  168.   getsym("$illegal.")->class = STOKEN;
  169.   /* Read the declaration section.  Copy %{ ... %} groups to ftable and fdefines file.
  170.      Also notice any %token, %left, etc. found there.  */
  171.   fprintf(ftable, "\n/*  A Bison parser, made from %s  */\n\n", infile);
  172.   fprintf(ftable, "#define YYBISON 1  /* Identify Bison output.  */\n\n");
  173.   read_declarations();
  174.   /* output the definition of YYLTYPE into the fattrs and fdefines files.  */
  175.   /* fattrs winds up in the .tab.c file, before bison.simple.  */
  176.   fprintf(fattrs, LTYPESTR);
  177.   /* start writing the guard and action files, if they are needed.  */
  178.   output_headers();
  179.   /* read in the grammar, build grammar in list form.  write out guards and actions.  */
  180.   readgram();
  181.   /* Now we know whether we need the line-number stack.
  182.      If we do, write its type into the .tab.h file.  */
  183.   if (yylsp_needed)
  184.     {
  185.       if (fdefines)
  186.     fprintf(fdefines, LTYPESTR);
  187.     }
  188.   /* write closing delimiters for actions and guards.  */
  189.   output_trailers();
  190.   if (yylsp_needed)
  191.     fprintf(ftable, "#define YYLSP_NEEDED\n\n");
  192.   /* assign the symbols their symbol numbers.
  193.      Write #defines for the token symbols into fdefines if requested.  */
  194.   packsymbols();
  195.   /* convert the grammar into the format described in gram.h.  */
  196.   packgram();
  197.   /* free the symbol table data structure
  198.      since symbols are now all referred to by symbol number.  */
  199.   free_symtab();
  200. }
  201.  
  202.  
  203.  
  204. /* read from finput until %% is seen.  Discard the %%.
  205. Handle any % declarations,
  206. and copy the contents of any %{ ... %} groups to fattrs.  */
  207.  
  208. void
  209. read_declarations ()
  210. {
  211.   register int c;
  212.   register int tok;
  213.  
  214.   for (;;)
  215.     {
  216.       c = skip_white_space();
  217.  
  218.       if (c == '%')
  219.     {
  220.       tok = parse_percent_token();
  221.  
  222.       switch (tok)
  223.         {
  224.         case TWO_PERCENTS:
  225.           return;
  226.  
  227.         case PERCENT_LEFT_CURLY:
  228.           copy_definition();
  229.           break;
  230.  
  231.         case TOKEN:
  232.           parse_token_decl (STOKEN, SNTERM);
  233.           break;
  234.     
  235.         case NTERM:
  236.           parse_token_decl (SNTERM, STOKEN);
  237.           break;
  238.     
  239.         case TYPE:
  240.           parse_type_decl();
  241.           break;
  242.     
  243.         case START:
  244.           parse_start_decl();
  245.           break;
  246.     
  247.         case UNION:
  248.           parse_union_decl();
  249.           break;
  250.     
  251.         case EXPECT:
  252.           parse_expect_decl();
  253.           break;
  254.     
  255.         case LEFT:
  256.           parse_assoc_decl(LEFT_ASSOC);
  257.           break;
  258.  
  259.         case RIGHT:
  260.           parse_assoc_decl(RIGHT_ASSOC);
  261.           break;
  262.  
  263.         case NONASSOC:
  264.           parse_assoc_decl(NON_ASSOC);
  265.           break;
  266.  
  267.         case SEMANTIC_PARSER:
  268.           if (semantic_parser == 0)
  269.         {
  270.           semantic_parser = 1;
  271.           open_extra_files();
  272.         }
  273.           break;
  274.  
  275.         case PURE_PARSER:
  276.           pure_parser = 1;
  277.           break;
  278.  
  279.         default:
  280.           fatal("junk after `%%' in definition section");
  281.         }
  282.     }
  283.       else if (c == EOF)
  284.         fatal("no input grammar");
  285.       else if (c >= 040 && c <= 0177)
  286.     fatals ("unknown character `%c' in declaration section", c);
  287.       else
  288.     fatals ("unknown character with code 0x%x in declaration section", c);
  289.     }
  290. }
  291.  
  292.  
  293. /* copy the contents of a %{ ... %} into the definitions file.
  294. The %{ has already been read.  Return after reading the %}.  */
  295.  
  296. void
  297. copy_definition ()
  298. {
  299.   register int c;
  300.   register int match;
  301.   register int ended;
  302.   register int after_percent;  /* -1 while reading a character if prev char was % */
  303.   int cplus_comment;
  304.  
  305.   if (!nolinesflag)
  306.     fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile);
  307.  
  308.   after_percent = 0;
  309.  
  310.   c = getc(finput);
  311.  
  312.   for (;;)
  313.     {
  314.       switch (c)
  315.     {
  316.     case '\n':
  317.       putc(c, fattrs);
  318.       lineno++;
  319.       break;
  320.  
  321.     case '%':
  322.           after_percent = -1;
  323.       break;
  324.           
  325.     case '\'':
  326.     case '"':
  327.       match = c;
  328.       putc(c, fattrs);
  329.       c = getc(finput);
  330.  
  331.       while (c != match)
  332.         {
  333.           if (c == EOF || c == '\n')
  334.         fatal("unterminated string");
  335.  
  336.           putc(c, fattrs);
  337.           
  338.           if (c == '\\')
  339.         {
  340.           c = getc(finput);
  341.           if (c == EOF)
  342.             fatal("unterminated string");
  343.           putc(c, fattrs);
  344.           if (c == '\n')
  345.             lineno++;
  346.         }
  347.  
  348.           c = getc(finput);
  349.         }
  350.  
  351.       putc(c, fattrs);
  352.       break;
  353.  
  354.     case '/':
  355.       putc(c, fattrs);
  356.       c = getc(finput);
  357.       if (c != '*' && c != '/')
  358.         continue;
  359.  
  360.       cplus_comment = (c == '/');
  361.       putc(c, fattrs);
  362.       c = getc(finput);
  363.  
  364.       ended = 0;
  365.       while (!ended)
  366.         {
  367.           if (!cplus_comment && c == '*')
  368.         {
  369.           while (c == '*')
  370.             {
  371.               putc(c, fattrs);
  372.               c = getc(finput);
  373.             }
  374.  
  375.           if (c == '/')
  376.             {
  377.               putc(c, fattrs);
  378.               ended = 1;
  379.             }
  380.         }
  381.           else if (c == '\n')
  382.         {
  383.           lineno++;
  384.           putc(c, fattrs);
  385.           if (cplus_comment)
  386.             ended = 1;
  387.           else
  388.             c = getc(finput);
  389.         }
  390.           else if (c == EOF)
  391.         fatal("unterminated comment in `%{' definition");
  392.           else
  393.         {
  394.           putc(c, fattrs);
  395.           c = getc(finput);
  396.         }
  397.         }
  398.  
  399.       break;
  400.  
  401.     case EOF:
  402.       fatal("unterminated `%{' definition");
  403.  
  404.     default:
  405.       putc(c, fattrs);
  406.     }
  407.  
  408.       c = getc(finput);
  409.  
  410.       if (after_percent)
  411.     {
  412.       if (c == '}')
  413.         return;
  414.       putc('%', fattrs);
  415.     }
  416.       after_percent = 0;
  417.  
  418.     }
  419.  
  420. }
  421.  
  422.  
  423.  
  424. /* parse what comes after %token or %nterm.
  425. For %token, what_is is STOKEN and what_is_not is SNTERM.
  426. For %nterm, the arguments are reversed.  */
  427.  
  428. void
  429. parse_token_decl (what_is, what_is_not)
  430.      int what_is, what_is_not;
  431. {
  432. /*   register int start_lineno; JF */
  433.   register int token = 0;
  434.   register int prev;
  435.   register char *typename = 0;
  436.   int k;
  437.  
  438. /*   start_lineno = lineno; JF */
  439.  
  440.   for (;;)
  441.     {
  442.       if(ungetc(skip_white_space(), finput) == '%')
  443.     return;
  444.  
  445. /*      if (lineno != start_lineno)
  446.     return; JF */
  447.  
  448.       /* we have not passed a newline, so the token now starting is in this declaration */
  449.       prev = token;
  450.  
  451.       token = lex();
  452.       if (token == COMMA)
  453.     continue;
  454.       if (token == TYPENAME)
  455.     {
  456.       k = strlen(token_buffer);
  457.       typename = NEW2(k + 1, char);
  458.       strcpy(typename, token_buffer);
  459.       value_components_used = 1;
  460.     }
  461.       else if (token == IDENTIFIER)
  462.     {
  463.       int oldclass = symval->class;
  464.  
  465.       if (symval->class == what_is_not)
  466.         fatals("symbol %s redefined", symval->tag);
  467.       symval->class = what_is;
  468.       if (what_is == SNTERM && oldclass != SNTERM)
  469.         symval->value = nvars++;
  470.  
  471.       if (typename)
  472.         {
  473.           if (symval->type_name == NULL)
  474.         symval->type_name = typename;
  475.           else
  476.         fatals("type redeclaration for %s", symval->tag);
  477.         }
  478.     }
  479.       else if (prev == IDENTIFIER && token == NUMBER)
  480.         {
  481.       symval->user_token_number = numval;
  482.       translations = 1;
  483.         }
  484.       else
  485.     fatal("invalid text in %token or %nterm declaration");
  486.     }
  487.  
  488. }
  489.  
  490.  
  491.  
  492. /* parse what comes after %start */
  493.  
  494. void
  495. parse_start_decl ()
  496. {
  497.   if (start_flag)
  498.     fatal("multiple %start declarations");
  499.   start_flag = 1;
  500.   if (lex() != IDENTIFIER)
  501.     fatal("invalid %start declaration");
  502.   startval = symval;
  503. }
  504.  
  505.  
  506.  
  507. /* read in a %type declaration and record its information for get_type_name to access */
  508.  
  509. void
  510. parse_type_decl ()
  511. {
  512.   register int k;
  513.   register char *name;
  514. /*   register int start_lineno; JF */
  515.  
  516.   if (lex() != TYPENAME)
  517.     fatal("ill-formed %type declaration");
  518.  
  519.   k = strlen(token_buffer);
  520.   name = NEW2(k + 1, char);
  521.   strcpy(name, token_buffer);
  522.  
  523. /*   start_lineno = lineno; */
  524.  
  525.   for (;;)
  526.     {
  527.       register int t;
  528.  
  529.       if(ungetc(skip_white_space(), finput) == '%')
  530.     return;
  531.  
  532. /*       if (lineno != start_lineno)
  533.     return; JF */
  534.  
  535.       /* we have not passed a newline, so the token now starting is in this declaration */
  536.  
  537.       t = lex();
  538.  
  539.       switch (t)
  540.     {
  541.  
  542.     case COMMA:
  543.     case SEMICOLON:
  544.       break;
  545.  
  546.     case IDENTIFIER:
  547.       if (symval->type_name == NULL)
  548.         symval->type_name = name;
  549.       else
  550.         fatals("type redeclaration for %s", symval->tag);
  551.  
  552.       break;
  553.  
  554.     default:
  555.       fatal("invalid %type declaration");
  556.     }
  557.     }
  558. }
  559.  
  560.  
  561.  
  562. /* read in a %left, %right or %nonassoc declaration and record its information.  */
  563. /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC.  */
  564.  
  565. void
  566. parse_assoc_decl (assoc)
  567. int assoc;
  568. {
  569.   register int k;
  570.   register char *name = NULL;
  571. /*  register int start_lineno; JF */
  572.   register int prev = 0;    /* JF added = 0 to keep lint happy */
  573.  
  574.   lastprec++;  /* Assign a new precedence level, never 0.  */
  575.  
  576. /*   start_lineno = lineno; */
  577.  
  578.   for (;;)
  579.     {
  580.       register int t;
  581.  
  582.       if(ungetc(skip_white_space(), finput) == '%')
  583.     return;
  584.  
  585.       /* if (lineno != start_lineno)
  586.     return; JF */
  587.  
  588.       /* we have not passed a newline, so the token now starting is in this declaration */
  589.  
  590.       t = lex();
  591.  
  592.       switch (t)
  593.     {
  594.  
  595.     case TYPENAME:
  596.       k = strlen(token_buffer);
  597.       name = NEW2(k + 1, char);
  598.       strcpy(name, token_buffer);
  599.       break;
  600.  
  601.     case COMMA:
  602.       break;
  603.  
  604.     case IDENTIFIER:
  605.       if (symval->prec != 0)
  606.         fatals("redefining precedence of %s", symval->tag);
  607.       symval->prec = lastprec;
  608.       symval->assoc = assoc;
  609.       if (symval->class == SNTERM)
  610.         fatals("symbol %s redefined", symval->tag);
  611.       symval->class = STOKEN;
  612.       if (name)
  613.         { /* record the type, if one is specified */
  614.           if (symval->type_name == NULL)
  615.         symval->type_name = name;
  616.           else
  617.         fatals("type redeclaration for %s", symval->tag);
  618.         }
  619.       break;
  620.  
  621.     case NUMBER:
  622.       if (prev == IDENTIFIER)
  623.             {
  624.           symval->user_token_number = numval;
  625.           translations = 1;
  626.             }
  627.           else      
  628.         fatal("invalid text in association declaration");
  629.       break;
  630.  
  631.     case SEMICOLON:
  632.       return;
  633.  
  634.     default:
  635.       fatal("malformatted association declaration");
  636.     }
  637.  
  638.       prev = t;
  639.  
  640.     }
  641. }
  642.  
  643.  
  644.  
  645. /* copy the union declaration into fattrs (and fdefines),
  646.    where it is made into the
  647.    definition of YYSTYPE, the type of elements of the parser value stack.  */
  648.  
  649. void
  650. parse_union_decl()
  651. {
  652.   register int c;
  653.   register int count;
  654.   register int in_comment;
  655.   int cplus_comment;
  656.  
  657.   if (typed)
  658.     fatal("multiple %union declarations");
  659.  
  660.   typed = 1;
  661.  
  662.   if (!nolinesflag)
  663.     fprintf(fattrs, "\n#line %d \"%s\"\n", lineno, infile);
  664.   else
  665.     fprintf(fattrs, "\n");
  666.  
  667.   fprintf(fattrs, "typedef union");
  668.   if (fdefines)
  669.     fprintf(fdefines, "typedef union");
  670.  
  671.   count = 0;
  672.   in_comment = 0;
  673.  
  674.   c = getc(finput);
  675.  
  676.   while (c != EOF)
  677.     {
  678.       putc(c, fattrs);
  679.       if (fdefines)
  680.     putc(c, fdefines);
  681.  
  682.       switch (c)
  683.     {
  684.     case '\n':
  685.       lineno++;
  686.       break;
  687.  
  688.     case '/':
  689.       c = getc(finput);
  690.       if (c != '*' && c != '/')
  691.         ungetc(c, finput);
  692.       else
  693.         {
  694.           putc(c, fattrs);
  695.           if (fdefines)
  696.         putc(c, fdefines);
  697.           cplus_comment = (c == '/');
  698.           in_comment = 1;
  699.           c = getc(finput);
  700.           while (in_comment)
  701.         {
  702.           if (c == '\n')
  703.             {
  704.               lineno++;
  705.               if (cplus_comment)
  706.             in_comment = 0;
  707.             }
  708.           if (c == EOF)
  709.             fatal("unterminated comment");
  710.  
  711.           putc(c, fattrs);
  712.           if (fdefines)
  713.             putc(c, fdefines);
  714.           if (!cplus_comment && c == '*')
  715.             {
  716.               c = getc(finput);
  717.               if (c == '/')
  718.             {
  719.               putc('/', fattrs);
  720.               if (fdefines)
  721.                 putc('/', fdefines);
  722.               in_comment = 0;
  723.             }
  724.             }
  725.           else
  726.             c = getc(finput);
  727.         }
  728.         }
  729.       break;
  730.  
  731.  
  732.     case '{':
  733.       count++;
  734.       break;
  735.  
  736.     case '}':
  737.       if (count == 0)
  738.         fatal ("unmatched close-brace (`}')");
  739.       count--;
  740.       if (count == 0)
  741.         {
  742.           fprintf(fattrs, " YYSTYPE;\n");
  743.           if (fdefines)
  744.         fprintf(fdefines, " YYSTYPE;\n");
  745.           /* JF don't choke on trailing semi */
  746.           c=skip_white_space();
  747.           if(c!=';') ungetc(c,finput);
  748.           return;
  749.         }
  750.     }
  751.  
  752.       c = getc(finput);
  753.     }
  754. }
  755.  
  756. /* parse the declaration %expect N which says to expect N
  757.    shift-reduce conflicts.  */
  758.  
  759. void
  760. parse_expect_decl()
  761. {
  762.   register int c;
  763.   register int count;
  764.   char buffer[20];
  765.  
  766.   c = getc(finput);
  767.   while (c == ' ' || c == '\t')
  768.     c = getc(finput);
  769.  
  770.   count = 0;
  771.   while (c >= '0' && c <= '9')
  772.     {
  773.       if (count < 20)
  774.     buffer[count++] = c;
  775.       c = getc(finput);
  776.     }
  777.   buffer[count] = 0;
  778.  
  779.   ungetc (c, finput);
  780.  
  781.   expected_conflicts = atoi (buffer);
  782. }
  783.  
  784. /* that's all of parsing the declaration section */
  785.  
  786. /* Get the data type (alternative in the union) of the value for symbol n in rule rule.  */
  787.  
  788. char *
  789. get_type_name(n, rule)
  790. int n;
  791. symbol_list *rule;
  792. {
  793.   static char *msg = "invalid $ value";
  794.  
  795.   register int i;
  796.   register symbol_list *rp;
  797.  
  798.   if (n < 0)
  799.     fatal(msg);
  800.  
  801.   rp = rule;
  802.   i = 0;
  803.  
  804.   while (i < n)
  805.     {
  806.       rp = rp->next;
  807.       if (rp == NULL || rp->sym == NULL)
  808.     fatal(msg);
  809.       i++;
  810.     }
  811.  
  812.   return (rp->sym->type_name);
  813. }
  814.  
  815.  
  816.  
  817. /* after %guard is seen in the input file,
  818. copy the actual guard into the guards file.
  819. If the guard is followed by an action, copy that into the actions file.
  820. stack_offset is the number of values in the current rule so far,
  821. which says where to find $0 with respect to the top of the stack,
  822. for the simple parser in which the stack is not popped until after the guard is run.  */
  823.  
  824. void
  825. copy_guard(rule, stack_offset)
  826. symbol_list *rule;
  827. int stack_offset;
  828. {
  829.   register int c;
  830.   register int n;
  831.   register int count;
  832.   register int match;
  833.   register int ended;
  834.   register char *type_name;
  835.   int brace_flag = 0;
  836.   int cplus_comment;
  837.  
  838.   /* offset is always 0 if parser has already popped the stack pointer */
  839.   if (semantic_parser) stack_offset = 0;
  840.  
  841.   fprintf(fguard, "\ncase %d:\n", nrules);
  842.   if (!nolinesflag)
  843.     fprintf(fguard, "#line %d \"%s\"\n", lineno, infile);
  844.   putc('{', fguard);
  845.  
  846.   count = 0;
  847.   c = getc(finput);
  848.  
  849.   while (brace_flag ? (count > 0) : (c != ';'))
  850.     {
  851.       switch (c)
  852.     {
  853.     case '\n':
  854.       putc(c, fguard);
  855.       lineno++;
  856.       break;
  857.  
  858.     case '{':
  859.       putc(c, fguard);
  860.       brace_flag = 1;
  861.       count++;
  862.       break;
  863.  
  864.     case '}':
  865.       putc(c, fguard);
  866.       if (count > 0)
  867.         count--;
  868.       else
  869.         fatal("unmatched right brace ('}')");
  870.           break;
  871.  
  872.     case '\'':
  873.     case '"':
  874.       match = c;
  875.       putc(c, fguard);
  876.       c = getc(finput);
  877.  
  878.       while (c != match)
  879.         {
  880.           if (c == EOF || c == '\n')
  881.         fatal("unterminated string");
  882.  
  883.           putc(c, fguard);
  884.           
  885.           if (c == '\\')
  886.         {
  887.           c = getc(finput);
  888.           if (c == EOF)
  889.             fatal("unterminated string");
  890.           putc(c, fguard);
  891.           if (c == '\n')
  892.             lineno++;
  893.         }
  894.  
  895.           c = getc(finput);
  896.         }
  897.  
  898.       putc(c, fguard);
  899.       break;
  900.  
  901.     case '/':
  902.       putc(c, fguard);
  903.       c = getc(finput);
  904.       if (c != '*' && c != '/');
  905.         continue;
  906.  
  907.       cplus_comment = (c == '/');
  908.       putc(c, fguard);
  909.       c = getc(finput);
  910.  
  911.       ended = 0;
  912.       while (!ended)
  913.         {
  914.           if (!cplus_comment && c == '*')
  915.         {
  916.           while (c == '*')
  917.             {
  918.               putc(c, fguard);
  919.               c = getc(finput);
  920.             }
  921.  
  922.           if (c == '/')
  923.             {
  924.               putc(c, fguard);
  925.               ended = 1;
  926.             }
  927.         }
  928.           else if (c == '\n')
  929.         {
  930.           lineno++;
  931.           putc(c, fguard);
  932.           if (cplus_comment)
  933.             ended = 1;
  934.           else
  935.             c = getc(finput);
  936.         }
  937.           else if (c == EOF)
  938.         fatal("unterminated comment");
  939.           else
  940.         {
  941.           putc(c, fguard);
  942.           c = getc(finput);
  943.         }
  944.         }
  945.  
  946.       break;
  947.  
  948.     case '$':
  949.       c = getc(finput);
  950.       type_name = NULL;
  951.  
  952.       if (c == '<')
  953.         {
  954.           register char *cp = token_buffer;
  955.  
  956.           while ((c = getc(finput)) != '>' && c > 0)
  957.         *cp++ = c;
  958.           *cp = 0;
  959.           type_name = token_buffer;
  960.  
  961.           c = getc(finput);
  962.         }
  963.  
  964.       if (c == '$')
  965.         {
  966.           fprintf(fguard, "yyval");
  967.           if (!type_name) type_name = rule->sym->type_name;
  968.           if (type_name)
  969.         fprintf(fguard, ".%s", type_name);
  970.           if(!type_name && typed)    /* JF */
  971.         fprintf(stderr,"%s:%d:  warning:  $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
  972.         }
  973.  
  974.       else if (isdigit(c) || c == '-')
  975.         {
  976.           ungetc (c, finput);
  977.           n = read_signed_integer(finput);
  978.           c = getc(finput);
  979.  
  980.           if (!type_name && n > 0)
  981.         type_name = get_type_name(n, rule);
  982.  
  983.           fprintf(fguard, "yyvsp[%d]", n - stack_offset);
  984.           if (type_name)
  985.         fprintf(fguard, ".%s", type_name);
  986.           if(!type_name && typed)    /* JF */
  987.         fprintf(stderr,"%s:%d:  warning:  $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
  988.           continue;
  989.         }
  990.       else
  991.         fatals("$%c is invalid",c);    /* JF changed style */
  992.  
  993.       break;
  994.  
  995.     case '@':
  996.       c = getc(finput);
  997.       if (isdigit(c) || c == '-')
  998.         {
  999.           ungetc (c, finput);
  1000.           n = read_signed_integer(finput);
  1001.           c = getc(finput);
  1002.         }
  1003.       else
  1004.         fatals("@%c is invalid",c);    /* JF changed style */
  1005.  
  1006.       fprintf(fguard, "yylsp[%d]", n - stack_offset);
  1007.       yylsp_needed = 1;
  1008.  
  1009.       continue;
  1010.  
  1011.     case EOF:
  1012.       fatal("unterminated %guard clause");
  1013.  
  1014.     default:
  1015.       putc(c, fguard);
  1016.     }
  1017.  
  1018.       if (c != '}' || count != 0)
  1019.     c = getc(finput);
  1020.     }
  1021.  
  1022.   c = skip_white_space();
  1023.  
  1024.   fprintf(fguard, ";\n    break;}");
  1025.   if (c == '{')
  1026.     copy_action(rule, stack_offset);
  1027.   else if (c == '=')
  1028.     {
  1029.       c = getc(finput);
  1030.       if (c == '{')
  1031.     copy_action(rule, stack_offset);
  1032.     }
  1033.   else
  1034.     ungetc(c, finput);
  1035. }
  1036.  
  1037.  
  1038.  
  1039. /* Assuming that a { has just been seen, copy everything up to the matching }
  1040. into the actions file.
  1041. stack_offset is the number of values in the current rule so far,
  1042. which says where to find $0 with respect to the top of the stack.  */
  1043.  
  1044. void
  1045. copy_action(rule, stack_offset)
  1046. symbol_list *rule;
  1047. int stack_offset;
  1048. {
  1049.   register int c;
  1050.   register int n;
  1051.   register int count;
  1052.   register int match;
  1053.   register int ended;
  1054.   register char *type_name;
  1055.   int cplus_comment;
  1056.  
  1057.   /* offset is always 0 if parser has already popped the stack pointer */
  1058.   if (semantic_parser) stack_offset = 0;
  1059.  
  1060.   fprintf(faction, "\ncase %d:\n", nrules);
  1061.   if (!nolinesflag)
  1062.     fprintf(faction, "#line %d \"%s\"\n", lineno, infile);
  1063.   putc('{', faction);
  1064.  
  1065.   count = 1;
  1066.   c = getc(finput);
  1067.  
  1068.   while (count > 0)
  1069.     {
  1070.       while (c != '}')
  1071.         {
  1072.           switch (c)
  1073.         {
  1074.         case '\n':
  1075.           putc(c, faction);
  1076.           lineno++;
  1077.           break;
  1078.  
  1079.         case '{':
  1080.           putc(c, faction);
  1081.           count++;
  1082.           break;
  1083.  
  1084.         case '\'':
  1085.         case '"':
  1086.           match = c;
  1087.           putc(c, faction);
  1088.           c = getc(finput);
  1089.  
  1090.           while (c != match)
  1091.         {
  1092.           if (c == EOF || c == '\n')
  1093.             fatal("unterminated string");
  1094.  
  1095.           putc(c, faction);
  1096.  
  1097.           if (c == '\\')
  1098.             {
  1099.               c = getc(finput);
  1100.               if (c == EOF)
  1101.             fatal("unterminated string");
  1102.               putc(c, faction);
  1103.               if (c == '\n')
  1104.             lineno++;
  1105.             }
  1106.  
  1107.           c = getc(finput);
  1108.         }
  1109.  
  1110.           putc(c, faction);
  1111.           break;
  1112.  
  1113.         case '/':
  1114.           putc(c, faction);
  1115.           c = getc(finput);
  1116.           if (c != '*' && c != '/')
  1117.         continue;
  1118.  
  1119.           cplus_comment = (c == '/');
  1120.           putc(c, faction);
  1121.           c = getc(finput);
  1122.  
  1123.           ended = 0;
  1124.           while (!ended)
  1125.         {
  1126.           if (!cplus_comment && c == '*')
  1127.             {
  1128.               while (c == '*')
  1129.                 {
  1130.               putc(c, faction);
  1131.               c = getc(finput);
  1132.             }
  1133.  
  1134.               if (c == '/')
  1135.             {
  1136.               putc(c, faction);
  1137.               ended = 1;
  1138.             }
  1139.             }
  1140.           else if (c == '\n')
  1141.             {
  1142.               lineno++;
  1143.               putc(c, faction);
  1144.               if (cplus_comment)
  1145.             ended = 1;
  1146.               else
  1147.                 c = getc(finput);
  1148.             }
  1149.           else if (c == EOF)
  1150.             fatal("unterminated comment");
  1151.           else
  1152.             {
  1153.               putc(c, faction);
  1154.               c = getc(finput);
  1155.             }
  1156.         }
  1157.  
  1158.           break;
  1159.  
  1160.         case '$':
  1161.           c = getc(finput);
  1162.           type_name = NULL;
  1163.  
  1164.           if (c == '<')
  1165.         {
  1166.           register char *cp = token_buffer;
  1167.  
  1168.           while ((c = getc(finput)) != '>' && c > 0)
  1169.             *cp++ = c;
  1170.           *cp = 0;
  1171.           type_name = token_buffer;
  1172.           value_components_used = 1;
  1173.  
  1174.           c = getc(finput);
  1175.         }
  1176.           if (c == '$')
  1177.         {
  1178.           fprintf(faction, "yyval");
  1179.           if (!type_name) type_name = get_type_name(0, rule);
  1180.           if (type_name)
  1181.             fprintf(faction, ".%s", type_name);
  1182.           if(!type_name && typed)    /* JF */
  1183.             fprintf(stderr,"%s:%d:  warning:  $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
  1184.         }
  1185.           else if (isdigit(c) || c == '-')
  1186.         {
  1187.           ungetc (c, finput);
  1188.           n = read_signed_integer(finput);
  1189.           c = getc(finput);
  1190.  
  1191.           if (!type_name && n > 0)
  1192.             type_name = get_type_name(n, rule);
  1193.  
  1194.           fprintf(faction, "yyvsp[%d]", n - stack_offset);
  1195.           if (type_name)
  1196.             fprintf(faction, ".%s", type_name);
  1197.           if(!type_name && typed)    /* JF */
  1198.             fprintf(stderr,"%s:%d:  warning:  $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
  1199.           continue;
  1200.         }
  1201.           else
  1202.         fatals("$%c is invalid",c);    /* JF changed format */
  1203.  
  1204.           break;
  1205.  
  1206.         case '@':
  1207.           c = getc(finput);
  1208.           if (isdigit(c) || c == '-')
  1209.         {
  1210.           ungetc (c, finput);
  1211.           n = read_signed_integer(finput);
  1212.           c = getc(finput);
  1213.         }
  1214.           else
  1215.         fatal("invalid @-construct");
  1216.  
  1217.           fprintf(faction, "yylsp[%d]", n - stack_offset);
  1218.           yylsp_needed = 1;
  1219.  
  1220.           continue;
  1221.  
  1222.         case EOF:
  1223.           fatal("unmatched '{'");
  1224.  
  1225.         default:
  1226.           putc(c, faction);
  1227.         }
  1228.  
  1229.           c = getc(finput);
  1230.         }
  1231.  
  1232.       /* above loop exits when c is '}' */
  1233.  
  1234.       if (--count)
  1235.         {
  1236.       putc(c, faction);
  1237.       c = getc(finput);
  1238.     }
  1239.     }
  1240.  
  1241.   fprintf(faction, ";\n    break;}");
  1242. }
  1243.  
  1244.  
  1245.  
  1246. /* generate a dummy symbol, a nonterminal,
  1247. whose name cannot conflict with the user's names. */
  1248.  
  1249. bucket *
  1250. gensym()
  1251. {
  1252.   register bucket *sym;
  1253.  
  1254.   sprintf (token_buffer, "@%d", ++gensym_count);
  1255.   sym = getsym(token_buffer);
  1256.   sym->class = SNTERM;
  1257.   sym->value = nvars++;
  1258.   return (sym);
  1259. }
  1260.  
  1261. /* Parse the input grammar into a one symbol_list structure.
  1262. Each rule is represented by a sequence of symbols: the left hand side
  1263. followed by the contents of the right hand side, followed by a null pointer
  1264. instead of a symbol to terminate the rule.
  1265. The next symbol is the lhs of the following rule.
  1266.  
  1267. All guards and actions are copied out to the appropriate files,
  1268. labelled by the rule number they apply to.  */
  1269.  
  1270. void
  1271. readgram()
  1272. {
  1273.   register int t;
  1274.   register bucket *lhs;
  1275.   register symbol_list *p;
  1276.   register symbol_list *p1;
  1277.   register bucket *bp;
  1278.  
  1279.   symbol_list *crule;    /* points to first symbol_list of current rule.  */
  1280.             /* its symbol is the lhs of the rule.   */
  1281.   symbol_list *crule1;  /* points to the symbol_list preceding crule.  */
  1282.  
  1283.   p1 = NULL;
  1284.  
  1285.   t = lex();
  1286.  
  1287.   while (t != TWO_PERCENTS && t != ENDFILE)
  1288.     {
  1289.       if (t == IDENTIFIER || t == BAR)
  1290.     {
  1291.       register int actionflag = 0;
  1292.       int rulelength = 0;  /* number of symbols in rhs of this rule so far  */
  1293.       int xactions = 0;    /* JF for error checking */
  1294.       bucket *first_rhs = 0;
  1295.  
  1296.       if (t == IDENTIFIER)
  1297.         {
  1298.           lhs = symval;
  1299.     
  1300.           t = lex();
  1301.           if (t != COLON)
  1302.         fatal("ill-formed rule");
  1303.         }
  1304.  
  1305.       if (nrules == 0)
  1306.         {
  1307.           if (t == BAR)
  1308.         fatal("grammar starts with vertical bar");
  1309.  
  1310.           if (!start_flag)
  1311.         startval = lhs;
  1312.         }
  1313.  
  1314.       /* start a new rule and record its lhs.  */
  1315.  
  1316.       nrules++;
  1317.       nitems++;
  1318.  
  1319.       record_rule_line ();
  1320.  
  1321.       p = NEW(symbol_list);
  1322.       p->sym = lhs;
  1323.  
  1324.       crule1 = p1;
  1325.       if (p1)
  1326.         p1->next = p;
  1327.       else
  1328.         grammar = p;
  1329.  
  1330.       p1 = p;
  1331.       crule = p;
  1332.  
  1333.       /* mark the rule's lhs as a nonterminal if not already so.  */
  1334.  
  1335.       if (lhs->class == SUNKNOWN)
  1336.         {
  1337.           lhs->class = SNTERM;
  1338.           lhs->value = nvars;
  1339.           nvars++;
  1340.         }
  1341.       else if (lhs->class == STOKEN)
  1342.         fatals("rule given for %s, which is a token", lhs->tag);
  1343.  
  1344.       /* read the rhs of the rule.  */
  1345.  
  1346.       for (;;)
  1347.         {
  1348.           t = lex();
  1349.  
  1350.           if (! (t == IDENTIFIER || t == LEFT_CURLY)) break;
  1351.  
  1352.           /* If next token is an identifier, see if a colon follows it.
  1353.          If one does, exit this rule now.  */
  1354.           if (t == IDENTIFIER)
  1355.         {
  1356.           register bucket *ssave;
  1357.           register int t1;
  1358.  
  1359.           ssave = symval;
  1360.           t1 = lex();
  1361.           unlex(t1);
  1362.           symval = ssave;
  1363.           if (t1 == COLON) break;
  1364.  
  1365.           if(!first_rhs)    /* JF */
  1366.             first_rhs = symval;
  1367.           /* Not followed by colon =>
  1368.              process as part of this rule's rhs.  */
  1369.         }
  1370.  
  1371.           /* If we just passed an action, that action was in the middle
  1372.          of a rule, so make a dummy rule to reduce it to a
  1373.          non-terminal.  */
  1374.           if (actionflag)
  1375.         {
  1376.           register bucket *sdummy;
  1377.  
  1378.           /* Since the action was written out with this rule's */
  1379.           /* number, we must write give the new rule this number */
  1380.           /* by inserting the new rule before it.  */
  1381.  
  1382.           /* Make a dummy nonterminal, a gensym.  */
  1383.           sdummy = gensym();
  1384.  
  1385.           /* Make a new rule, whose body is empty,
  1386.              before the current one, so that the action
  1387.              just read can belong to it.  */
  1388.           nrules++;
  1389.           nitems++;
  1390.           record_rule_line ();
  1391.           p = NEW(symbol_list);
  1392.           if (crule1)
  1393.             crule1->next = p;
  1394.           else grammar = p;
  1395.           p->sym = sdummy;
  1396.           crule1 = NEW(symbol_list);
  1397.           p->next = crule1;
  1398.           crule1->next = crule;
  1399.  
  1400.           /* insert the dummy generated by that rule into this rule.  */
  1401.           nitems++;
  1402.           p = NEW(symbol_list);
  1403.           p->sym = sdummy;
  1404.           p1->next = p;
  1405.           p1 = p;
  1406.  
  1407.           actionflag = 0;
  1408.         }
  1409.  
  1410.           if (t == IDENTIFIER)
  1411.         {
  1412.           nitems++;
  1413.           p = NEW(symbol_list);
  1414.           p->sym = symval;
  1415.           p1->next = p;
  1416.           p1 = p;
  1417.         }
  1418.           else /* handle an action.  */
  1419.         {
  1420.           copy_action(crule, rulelength);
  1421.           actionflag = 1;
  1422.           xactions++;    /* JF */
  1423.         }
  1424.           rulelength++;
  1425.         }
  1426.  
  1427.       /* Put an empty link in the list to mark the end of this rule  */
  1428.       p = NEW(symbol_list);
  1429.       p1->next = p;
  1430.       p1 = p;
  1431.  
  1432.       if (t == PREC)
  1433.         {
  1434.           t = lex();
  1435.           crule->ruleprec = symval;
  1436.           t = lex();
  1437.         }
  1438.       if (t == GUARD)
  1439.         {
  1440.           if (! semantic_parser)
  1441.         fatal("%guard present but %semantic_parser not specified");
  1442.  
  1443.           copy_guard(crule, rulelength);
  1444.           t = lex();
  1445.         }
  1446.       else if (t == LEFT_CURLY)
  1447.         {
  1448.           if (actionflag) fatal("two actions at end of one rule");
  1449.           copy_action(crule, rulelength);
  1450.           t = lex();
  1451.         }
  1452.       /* If $$ is being set in default way,
  1453.          warn if any type mismatch.  */
  1454.       else if (!xactions && first_rhs && lhs->type_name != first_rhs->type_name)
  1455.         {
  1456.           if (lhs->type_name == 0 || first_rhs->type_name == 0
  1457.           || strcmp(lhs->type_name,first_rhs->type_name))
  1458.         fprintf(stderr, "%s:%d:  warning:  type clash ('%s' '%s') on default action\n",
  1459.             infile,
  1460.             lineno,
  1461.             lhs->type_name ? lhs->type_name : "",
  1462.             first_rhs->type_name ? first_rhs->type_name : "");
  1463.         }
  1464.       /* Warn if there is no default for $$ but we need one.  */
  1465.       else if (!xactions && !first_rhs && lhs->type_name != 0)
  1466.         fprintf(stderr,
  1467.             "%s:%d:  warning: empty rule for typed nonterminal, and no action\n",
  1468.             infile,
  1469.             lineno);
  1470.       if (t == SEMICOLON)
  1471.         t = lex();
  1472.     }
  1473.       /* these things can appear as alternatives to rules.  */
  1474.       else if (t == TOKEN)
  1475.     {
  1476.       parse_token_decl(STOKEN, SNTERM);
  1477.       t = lex();
  1478.     }
  1479.       else if (t == NTERM)
  1480.     {
  1481.       parse_token_decl(SNTERM, STOKEN);
  1482.       t = lex();
  1483.     }
  1484.       else if (t == TYPE)
  1485.     {
  1486.       t = get_type();
  1487.     }
  1488.       else if (t == UNION)
  1489.     {
  1490.       parse_union_decl();
  1491.       t = lex();
  1492.     }
  1493.       else if (t == EXPECT)
  1494.     {
  1495.       parse_expect_decl();
  1496.       t = lex();
  1497.     }
  1498.       else if (t == START)
  1499.     {
  1500.       parse_start_decl();
  1501.       t = lex();
  1502.     }
  1503.       else
  1504.     fatal("invalid input");
  1505.     }
  1506.  
  1507.   if (nsyms > MAXSHORT)
  1508.     fatals("too many symbols (tokens plus nonterminals); maximum %d",
  1509.        MAXSHORT);
  1510.   if (nrules == 0)
  1511.     fatal("no input grammar");
  1512.  
  1513.   if (typed == 0    /* JF put out same default YYSTYPE as YACC does */
  1514.       && !value_components_used)
  1515.     {
  1516.       /* We used to use `unsigned long' as YYSTYPE on MSDOS,
  1517.      but it seems better to be consistent.
  1518.      Most programs should declare their own type anyway.  */
  1519.       fprintf(fattrs, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
  1520.       if (fdefines)
  1521.     fprintf(fdefines, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
  1522.     }
  1523.  
  1524.   /* Report any undefined symbols and consider them nonterminals.  */
  1525.  
  1526.   for (bp = firstsymbol; bp; bp = bp->next)
  1527.     if (bp->class == SUNKNOWN)
  1528.       {
  1529.     fprintf(stderr, "symbol %s used, not defined as token, and no rules for it\n",
  1530.             bp->tag);
  1531.     failure = 1;
  1532.     bp->class = SNTERM;
  1533.     bp->value = nvars++;
  1534.       }
  1535.  
  1536.   ntokens = nsyms - nvars;
  1537. }
  1538.  
  1539.  
  1540. void
  1541. record_rule_line ()
  1542. {
  1543.   /* Record each rule's source line number in rline table.  */
  1544.  
  1545.   if (nrules >= rline_allocated)
  1546.     {
  1547.       rline_allocated = nrules * 2;
  1548.       rline = (short *) realloc (rline,
  1549.                  rline_allocated * sizeof (short));
  1550.       if (rline == 0)
  1551.     {
  1552.       fprintf (stderr, "%s: memory exhausted\n", program_name);
  1553.       done (1);
  1554.     }
  1555.     }
  1556.   rline[nrules] = lineno;
  1557. }
  1558.  
  1559.  
  1560. /* read in a %type declaration and record its information for get_type_name to access */
  1561.  
  1562. int
  1563. get_type()
  1564. {
  1565.   register int k;
  1566.   register int t;
  1567.   register char *name;
  1568.  
  1569.   t = lex();
  1570.  
  1571.   if (t != TYPENAME)
  1572.     fatal("ill-formed %type declaration");
  1573.  
  1574.   k = strlen(token_buffer);
  1575.   name = NEW2(k + 1, char);
  1576.   strcpy(name, token_buffer);
  1577.  
  1578.   for (;;)
  1579.     {
  1580.       t = lex();
  1581.  
  1582.       switch (t)
  1583.     {
  1584.     case SEMICOLON:
  1585.       return (lex());
  1586.  
  1587.     case COMMA:
  1588.       break;
  1589.  
  1590.     case IDENTIFIER:
  1591.       if (symval->type_name == NULL)
  1592.         symval->type_name = name;
  1593.       else
  1594.         fatals("type redeclaration for %s", symval->tag);
  1595.  
  1596.       break;
  1597.  
  1598.     default:
  1599.       return (t);
  1600.     }
  1601.     }
  1602. }
  1603.  
  1604.  
  1605.  
  1606. /* assign symbol numbers, and write definition of token names into fdefines.
  1607. Set up vectors tags and sprec of names and precedences of symbols.  */
  1608.  
  1609. void
  1610. packsymbols()
  1611. {
  1612.   register bucket *bp;
  1613.   register int tokno = 1;
  1614.   register int i;
  1615.   register int last_user_token_number;
  1616.  
  1617.   /* int lossage = 0; JF set but not used */
  1618.  
  1619.   tags = NEW2(nsyms + 1, char *);
  1620.   tags[0] = "$";
  1621.  
  1622.   sprec = NEW2(nsyms, short);
  1623.   sassoc = NEW2(nsyms, short);
  1624.  
  1625.   max_user_token_number = 256;
  1626.   last_user_token_number = 256;
  1627.  
  1628.   for (bp = firstsymbol; bp; bp = bp->next)
  1629.     {
  1630.       if (bp->class == SNTERM)
  1631.     {
  1632.       bp->value += ntokens;
  1633.     }
  1634.       else
  1635.     {
  1636.       if (translations && !(bp->user_token_number))
  1637.         bp->user_token_number = ++last_user_token_number;
  1638.       if (bp->user_token_number > max_user_token_number)
  1639.         max_user_token_number = bp->user_token_number;
  1640.       bp->value = tokno++;
  1641.     }
  1642.  
  1643.       tags[bp->value] = bp->tag;
  1644.       sprec[bp->value] = bp->prec;
  1645.       sassoc[bp->value] = bp->assoc;
  1646.  
  1647.     }
  1648.  
  1649.   if (translations)
  1650.     {
  1651.       register int i;
  1652.  
  1653.       token_translations = NEW2(max_user_token_number+1, short);
  1654.  
  1655.       /* initialize all entries for literal tokens to 2,
  1656.      the internal token number for $illegal., which represents all invalid inputs.  */
  1657.       for (i = 0; i <= max_user_token_number; i++)
  1658.         token_translations[i] = 2;      
  1659.     }
  1660.  
  1661.   for (bp = firstsymbol; bp; bp = bp->next)
  1662.     {
  1663.       if (bp->value >= ntokens) continue;
  1664.       if (translations)
  1665.     {
  1666.       if (token_translations[bp->user_token_number] != 2)
  1667.         {
  1668.             /* JF made this a call to fatals() */
  1669.           fatals( "tokens %s and %s both assigned number %d",
  1670.                   tags[token_translations[bp->user_token_number]],
  1671.                   bp->tag,
  1672.                   bp->user_token_number);
  1673.         }
  1674.       token_translations[bp->user_token_number] = bp->value;
  1675.     }
  1676.     }
  1677.  
  1678.   error_token_number = errtoken->value;
  1679.  
  1680.   output_token_defines(ftable);
  1681.  
  1682.   if (startval->class == SUNKNOWN)
  1683.     fatals("the start symbol %s is undefined", startval->tag);
  1684.   else if (startval->class == STOKEN)
  1685.     fatals("the start symbol %s is a token", startval->tag);
  1686.  
  1687.   start_symbol = startval->value;
  1688.  
  1689.   if (definesflag)
  1690.     {
  1691.       output_token_defines(fdefines);
  1692.  
  1693.       if (!pure_parser)
  1694.     {
  1695.       if (spec_name_prefix)
  1696.         fprintf(fdefines, "\nextern YYSTYPE %slval;\n", spec_name_prefix);
  1697.       else
  1698.         fprintf(fdefines, "\nextern YYSTYPE yylval;\n");
  1699.     }
  1700.  
  1701.       if (semantic_parser)
  1702.     for (i = ntokens; i < nsyms; i++)
  1703.       {
  1704.         /* don't make these for dummy nonterminals made by gensym.  */
  1705.         if (*tags[i] != '@')
  1706.           fprintf(fdefines, "#define\tNT%s\t%d\n", tags[i], i);
  1707.       }
  1708.  
  1709.       fclose(fdefines);
  1710.       fdefines = NULL;
  1711.     }
  1712. }
  1713.       
  1714.  
  1715. void
  1716. output_token_defines(file)
  1717. FILE *file;
  1718. {
  1719.   bucket *bp;
  1720.  
  1721.   for (bp = firstsymbol; bp; bp = bp->next)
  1722.     {
  1723.       if (bp->value >= ntokens) continue;
  1724.  
  1725.       /* For named tokens, but not literal ones, define the name.  */
  1726.       /* The value is the user token number.  */
  1727.  
  1728.       if ('\'' != *tags[bp->value] && bp != errtoken)
  1729.     {
  1730.       register char *cp = tags[bp->value];
  1731.       register char c;
  1732.  
  1733.       /* Don't #define nonliteral tokens whose names contain periods.  */
  1734.  
  1735.       while ((c = *cp++) && c != '.');
  1736.       if (!c)
  1737.         {
  1738.               fprintf(file, "#define\t%s\t%d\n", tags[bp->value],
  1739.                 (translations ? bp->user_token_number : bp->value));
  1740.           if (semantic_parser)
  1741.                 fprintf(file, "#define\tT%s\t%d\n", tags[bp->value],
  1742.                   bp->value);
  1743.         }
  1744.     }
  1745.     }
  1746.  
  1747.   putc('\n', file);
  1748. }
  1749.  
  1750.  
  1751.  
  1752. /* convert the rules into the representation using rrhs, rlhs and ritems.  */
  1753.  
  1754. void
  1755. packgram()
  1756. {
  1757.   register int itemno;
  1758.   register int ruleno;
  1759.   register symbol_list *p;
  1760. /*  register bucket *bp; JF unused */
  1761.  
  1762.   bucket *ruleprec;
  1763.  
  1764.   ritem = NEW2(nitems + 1, short);
  1765.   rlhs = NEW2(nrules, short) - 1;
  1766.   rrhs = NEW2(nrules, short) - 1;
  1767.   rprec = NEW2(nrules, short) - 1;
  1768.   rprecsym = NEW2(nrules, short) - 1;
  1769.   rassoc = NEW2(nrules, short) - 1;
  1770.  
  1771.   itemno = 0;
  1772.   ruleno = 1;
  1773.  
  1774.   p = grammar;
  1775.   while (p)
  1776.     {
  1777.       rlhs[ruleno] = p->sym->value;
  1778.       rrhs[ruleno] = itemno;
  1779.       ruleprec = p->ruleprec;
  1780.  
  1781.       p = p->next;
  1782.       while (p && p->sym)
  1783.     {
  1784.       ritem[itemno++] = p->sym->value;
  1785.       /* A rule gets by default the precedence and associativity
  1786.          of the last token in it.  */
  1787.           if (p->sym->class == STOKEN)
  1788.         {
  1789.           rprec[ruleno] = p->sym->prec;
  1790.           rassoc[ruleno] = p->sym->assoc;
  1791.         }
  1792.       if (p) p = p->next;
  1793.     }
  1794.  
  1795.       /* If this rule has a %prec,
  1796.      the specified symbol's precedence replaces the default.  */
  1797.       if (ruleprec)
  1798.     {
  1799.           rprec[ruleno] = ruleprec->prec;
  1800.           rassoc[ruleno] = ruleprec->assoc;
  1801.       rprecsym[ruleno] = ruleprec->value;
  1802.     }
  1803.  
  1804.       ritem[itemno++] = -ruleno;
  1805.       ruleno++;
  1806.  
  1807.       if (p) p = p->next;
  1808.     }
  1809.  
  1810.   ritem[itemno] = 0;
  1811. }
  1812.  
  1813. /* Read a signed integer from STREAM and return its value.  */
  1814.  
  1815. int
  1816. read_signed_integer (stream)
  1817.      FILE *stream;
  1818. {
  1819.   register int c = getc(stream);
  1820.   register int sign = 1;
  1821.   register int n;
  1822.  
  1823.   if (c == '-')
  1824.     {
  1825.       c = getc(stream);
  1826.       sign = -1;
  1827.     }
  1828.   n = 0;
  1829.   while (isdigit(c))
  1830.     {
  1831.       n = 10*n + (c - '0');
  1832.       c = getc(stream);
  1833.     }
  1834.  
  1835.   ungetc(c, stream);
  1836.  
  1837.   return n * sign;
  1838. }
  1839.